#include<stdio.h>
#include<string.h>
#include"../include/main.h"
#include"../include/updatefile.h"
#include"../include/create_vfs.h"
#include"../include/mount_vfs.h"
#include"../include/narrtree.h"
#include"../include/hash.h"
#include"../include/constants.h"
#include"../include/vfs_errorcodes.h"

void update(char filepath[],char update_datapath[])
{
	llist *list;
	ntree *tofind=NULL;
	char update_data[BLOCK_SIZE];
	int blockno,ft=0;
	FILE *fp=fopen(update_datapath,"r");
	if(fp!=NULL)
	{	
		fseek(fp,0L,SEEK_END);
		ft=ftell(fp);
		fseek(fp,0,SEEK_SET);
	}

		
	if(mounted==0)
		printf("%s_FAILURE %s\n",glb_command,ERR_VFS_UPDATEFILE_04);
	else if(fp==NULL)
		printf("%s_FAILURE %s\n",glb_command,ERR_VFS_UPDATEFILE_02);
	else if(ft > BLOCK_SIZE)
		printf("%s_FAILURE %s\n",glb_command,ERR_VFS_UPDATEFILE_03);
	else
	{
		schflag=0;
		tofind=search_node(head,filepath);
		schflag=0;
		if(tofind==NULL)
			printf("%s_FAILURE %s\n",glb_command,ERR_VFS_UPDATEFILE_01);
		else
		{
			blockno=tofind->file_desc->blk_num;
			blockno=*(block_arr+blockno);
			//printf("data[%d]==%s\n",blockno,data[blockno]);
			fread(update_data,BLOCK_SIZE,1,fp);
			strcpy(data[blockno],update_data);
			data[blockno][strlen(update_data)-1]='\0';
			//printf("data[%d]==%s\n",blockno,data[blockno]);
			if(fp!=NULL)
			close(fp);
			printf("updatefile_SUCCESS\n");
		}
	}
}
